#include "../libxluploader.h"
#include "sites.h"
#include <assert.h>
#include <iostream>
/************************************************************************
 response:
 [<a href="http://www.imgplace.com/img403/350/17projects.png">
  	<img src="http://www.imgplace.com/img403/350/17projects.th.png" alt="17projects.png" class="thumb" />
 </a>] 
 <p class="title">17projects.png</p>
 <p class="imgurl">
 <strong>Link for photo</strong><br>
 <textarea name="url1" cols="60" rows="1" wrap="virtual" READONLY onfocus="javascript: this.select()">
	http://www.imgplace.com/viewimg403/350/17projects.png
 </textarea><br>
 <strong>Link for thumb</strong><br>
 <textarea name="url2" cols="60" rows="1" wrap="virtual" READONLY onfocus="javascript: this.select()">
 	http://www.imgplace.com/img403/350/17projects.th.png
 </textarea><br>
 <strong>Code to post the photo in a forum</strong><br>
 <textarea name="url3" cols="60" rows="2" wrap="virtual" READONLY onfocus="javascript: this.select()">
 	[URL=http://www.imgplace.com/viewimg403/350/17projects.png]
 	[img]http://www.imgplace.com/img403/350/17projects.th.png[/img][/URL]
 </textarea><br>
 <strong>Code to post photo in your website</strong><br>
 <textarea name="url4" cols="60" rows="2" wrap="virtual" READONLY onfocus="javascript: this.select()">
 	<a href="http://www.imgplace.com/viewimg403/350/17projects.png" target="_blank">
 		<img border="0" src="http://www.imgplace.com/img403/350/17projects.png" alt="free image hosting">
 	</a>
 </textarea><br>
 </p>
 <div class="space"></div>

 just find:
 http://www.imgplace.com/viewimg403/350/17projects.png

 we should extrack:
 --- http://img403.imageshack.us/img403/350/17projects.png


 ************************************************************************/
std::string CSiteImgPlace::parseImageUrl (const std::string &response) {
	if (!m_pcreCodePic) {
		return "";
	}

	std::string url = "http://";

	const int size = 30;
	int ov[size] = {0};
	const char *text = response.c_str();
	int res = pcre_exec(m_pcreCodePic, NULL, text, response.length(), 0, 0, ov, size);
	if (res != 4) {
		return "";
	}

	std::string cname = response.substr(ov[2], ov[3] - ov[2]);
	std::string dir = response.substr(ov[4], ov[5] - ov[4]);
	std::string name = response.substr(ov[6], ov[7] - ov[6]);
	url += cname;
	url += ".imageshack.us/";
	url += cname;
	url += "/";
	url += dir;
	url += "/";
	url += name;

	return url;
}

std::string CSiteImgPlace::getUrl() const {
	return "http://www.imgplace.com/";
}

CSiteImgPlace::CSiteImgPlace () {
	m_errPic = NULL;

	// >http://www.imgplace.com/viewimg403/350/17projects.png<
	const char *pattern_pic = ">http://www.imgplace.com/view(img[^/]+)/(\\d+)/([^<]+)";
	int flags = PCRE_BSR_ANYCRLF | PCRE_UTF8 | PCRE_MULTILINE | PCRE_DOTALL;
	m_pcreCodePic = pcre_compile(pattern_pic, flags, &m_errPic, &m_errPicOffset, NULL);
}

CSiteImgPlace::~CSiteImgPlace() {
	if (m_pcreCodePic) {
		pcre_free(m_pcreCodePic);
	}
}

TCHAR* CSiteImgPlace::getSiteName () {
	return _T("ImgPlace.com");
}


#ifdef DEBUG_SIZE_IMGPLACE
ISite* debugGetSite() {
	static CSiteImgPlace tp;
	return &tp;
}
#endif

